package com.deepglint.transform;

import com.deepglint.beans.SensorReading;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author mj
 * @version 1.0
 * @date 2021-11-15 21:35
 * reduce: 更加一般化的聚合操作
 */
public class TransformTest_Reduce {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        String inputPath = "C:\\Users\\马军\\Desktop\\Idea-workspace\\flink\\src\\main\\resources\\source.txt";
        DataStream<String> inputStream = env.readTextFile(inputPath);

        // 转换成SensorReading类型
        DataStream<SensorReading> streamMap = inputStream.map((MapFunction<String, SensorReading>) value -> {
            String[] split = value.split(",");
            return new SensorReading(split[0], split[1], new Long(split[2]), new Double(split[3]));
        });

        KeyedStream<SensorReading, String> keyBy = streamMap.keyBy(SensorReading::getId);

        // reduce聚合，取当前的最大值，以及当前最新的时间戳
        DataStream<SensorReading> resultStream = keyBy.reduce(new ReduceFunction<SensorReading>() {
            @Override
            public SensorReading reduce(SensorReading value1, SensorReading value2) throws Exception {
                return new SensorReading(value1.getId(), value1.getName(), value2.getTimestamp(), Math.max(value1.getTemperature(), value2.getTemperature()));
            }
        });

//        SingleOutputStreamOperator<SensorReading> redresultStreamuce = keyBy.reduce((v1, v2) -> new SensorReading(v1.getId(), v2.getName(), v2.getTimestamp(), Math.max(v1.getTemperature(), v2.getTemperature())));

        resultStream.print();

        env.execute();
    }
}